package com.hokook.task;

import com.hokook.delayed.DelayedPlainTask;
import com.hokook.entity.PlainTask;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.DelayQueue;

@Slf4j
public class PlainTaskRunable implements Runnable {

    /**
     *
     */
    private DelayQueue delayQueue;

    public PlainTaskRunable(DelayQueue delayQueue) {
        this.delayQueue = delayQueue;
    }

    @Override
    public void run() {
        log.info("[ 任务列表 ] -------->>>> {}", delayQueue.size());
        long startTime = System.currentTimeMillis();
        String taskName = "";
        while (delayQueue.peek() != null) {
            try {

                DelayedPlainTask delayedPlainTask = (DelayedPlainTask) delayQueue.take();
                PlainTask plainTask = delayedPlainTask.getPlainTask();
                taskName = plainTask.getTaskName();
                this.post(plainTask);
                this.updateExcuteStatus(plainTask);
            } catch (InterruptedException e) {
                log.error("[ 失败 ]");
            }
        }
        long end = System.currentTimeMillis();
        log.info("[ 耗费时间 ] --------->>>>> # {}    {}",taskName , (end - startTime));
    }

    private void updateExcuteStatus(PlainTask plainTask) {
        log.info("[ 更新状态 ] ------>>>> {}", plainTask.getTaskName());
    }

    private void post(PlainTask plainTask) {
        log.info("[ 代付状态 ] ------>>>> {}", plainTask.getTaskName());
    }


}
